﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

namespace Test
{
	/// <summary>
	/// Create throwaway instances of this helper class to go through the Expression tree to find
	/// instances of MemberExpression.
	/// </summary>
	internal class MemberExpressionFinder : ExpressionVisitor
	{
		List<MemberExpression> found;
		private MemberExpressionFinder() {
			this.found = new List<MemberExpression>();
		}

		public static IEnumerable<MemberExpression> Find(Expression expression) {
			var finder = new MemberExpressionFinder();
			finder.Visit(expression);
			return finder.found;
		}

		public override Expression Visit(Expression node) {
			return base.Visit(node);
		}
		protected override Expression VisitMember(MemberExpression node) {
			found.Add(node);
			return base.VisitMember(node);
		}
	}
}
